


var guigu = {
	winW:function(){return $(window).width();},
	winH:function(){return $(window).height();},
	getUrlData:function(dataName){
		function getUrlVars(){ 
			var vars = [], hash; 
			var hashes = window.location.href.slice(window.location.href.indexOf('?')+1).split('&'); 
			for(var i = 0; i < hashes.length; i++) { 
				hash = hashes[i].split('='); 
				vars.push(hash[0]); 
				vars[hash[0]] = hash[1]; 
			} 
			return vars; 
		} 
		var params = getUrlVars(); 
		for(var i=0;i<params.length;i++){
			if(params[i]==dataName){ 
				return decodeURI(params[params[i]]);
			}
		};
		return '';
  
	},
	
	loading:function(imgArr,callback,stopload){
		var imgArr = imgArr,
			loaded = 0,
			$loadRun = $('#loading_bg span'),
			$loadText = $('#loading_bg p');

		function callfn(){
			$('#loading_bg').addClass('loading_hide');
			if (typeof callback == 'function') {
				setTimeout(function(){
					callback();
				},200);
			};
		};
		
		function imgload(imgsrc){
			(function(imgsrc){

				var img = new Image();  
				img.onload = function(){
					loaded++;
					var jindu = parseInt(loaded*(100/imgArr.length));
					$loadText.text((jindu>100?100:jindu)+'%');
					$loadRun.css('width',jindu+'%');
					if(jindu>=100){ //加载完毕
						callfn();
					}
				};
				img.src = imgsrc; 

			})(imgsrc);
		};

		if (!stopload) {
			for(var i=0;i<imgArr.length;i++){
				imgload(imgArr[i]);
			}

			setTimeout(function(){
				if (loaded<imgArr.length) { //加载完毕
					callfn();
				};
			},15000);
		}else{
			callfn();
		};
		
	},
	yz:function($obj){
		
		for (var i = 0; i < $obj.length; i++) {
			var $thisObj = $obj.eq(i),
				thisVal = $thisObj.val(),
				thisType = $thisObj.attr('type-name');
			if (thisType=='name') {
				if (thisVal=='') {
					this.topTip('请输入姓名！');
					return false;
				}else if(thisVal.length<2 || thisVal.length>30){
					this.topTip('请输入正确的姓名！');
					return false;
				};
			}else if(thisType=='mobile'){
				var _mobile = /^1\d{10}$/;  //手机号正则
				if (thisVal=='') {
					this.topTip('请输入手机号！');
					return false;
				}else if(!_mobile.test(thisVal)){
					this.topTip('请输入正确的手机号！');
					return false;
				};
			}else if(thisType=='email'){
				var _mobile = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; 
				if (thisVal=='') {
					this.topTip('请输入邮箱！');
					return false;
				}else if(!_mobile.test(thisVal)){
					this.topTip('请输入正确的邮箱！');
					return false;
				};
			}else if(thisType=='address'){
				if (/选择/.test(thisVal)) {
					this.topTip(thisVal);
					return false;
				};
			}
		};

		return true;
	},
	showModal(opt){
		var self = this;
		var options = {
			content: ' ',
			showCancel: true,
			cancelFn:null,
			okFn:null,
			cancelText:'取消',
			okText:'确定',
			cancelGray: false, //取消按钮灰色，弱化
		};
		for(var key in opt){
			options[key] = opt[key];
		};
		let $cancelBtn = $('.js_alert_cancel'),
				$okBtn = $('.js_alert_ok');
		$cancelBtn.text(options.cancelText);
		$okBtn.text(options.okText);
		if(options.showCancel){
			$cancelBtn.show();
		}else{
			$cancelBtn.hide();
		}

		// 取消按钮颜色
		!options.cancelGray ? $cancelBtn.removeClass('grayBtn') : $cancelBtn.addClass('grayBtn');


		$(".js_alert_ok,.js_alert_cancel").unbind('touchend');
		$('.modal_box p').html(options.content);
		$('.modal_bg,.modal_box').show();

		
		if(!window.modalBind){
			$(document).on('touchend','.js_alert_ok',function(e){
				event.preventDefault();
				if(typeof options.okFn === 'function'){
					options.okFn();
				};
				self.hideModal();
			});

			$(document).on('touchend','.js_alert_cancel',function(e){
				event.preventDefault();
				if(typeof options.cancelFn === 'function'){
					options.cancelFn();
				};
				self.hideModal();
			});
		};
		window.modalBind = true;
	},
	hideModal(){
		$('.modal_bg,.modal_box').hide();
	},
	showLoading(){
		if(!document.querySelector('.js_loading_bg')){
			$('body').append('<div class="loading js_loading_bg"><div class="loading_box"><img src="images/loading.gif"></div></div>');
			document.querySelector('.js_loading_bg').addEventListener('touchmove',function(e){
				e.preventDefault();
			})
		}else{
			$('.js_loading_bg').show();
		}
		
	},
	hideLoading(){
		$('.js_loading_bg').hide();
	},
	share(fxInfo){
		var self = this;
		//拼团
		
		$.ajax({
			url: 'https://api.svenglish.cn/api/wechat/js-sdk-config?url=' + encodeURIComponent(location.href), // 仅为示例，并非真实的接口地址
			type: 'GET',
			success:function(data){
			
				if (data.code == 1) {
					wx.config({
						appId: data.data.appId, // 必填，公众号的唯一标识
						timestamp: data.data.timestamp, // 必填，生成签名的时间戳
						nonceStr: data.data.nonceStr, // 必填，生成签名的随机串
						signature: data.data.signature,// 必填，签名，见附录1
						jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
					});
					

					wx.ready(function () {
						wx.onMenuShareTimeline(fxInfo);
						wx.onMenuShareAppMessage(fxInfo);
					});
				}
			}
		});	
	},

};

